package com.sgb.member.view_model;

import androidx.annotation.NonNull;

import com.sgb.member.MemberConfig;
import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.ListMResponse;
import com.sgb.member.entity.MemberIntellectualPropertyBean;
import com.sgb.member.ui.fragment.IntellectualPropertyFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by：冯齐 on 2021/1/26 18:07
 * Description：
 */
public class IntellectualPropertyModel extends BaseViewModel {

    IntellectualPropertyFragment fragment;
    MemberRepertory repertory;
    private int current;

    public IntellectualPropertyModel(IntellectualPropertyFragment fragment, MemberRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    public void getMemberIntellectualPropertyRight(boolean isLoadMore, String companyName, String no1,
                                                   String no3, String no4, String no5, String no6, String no7,
                                                   String typesOfPropertyRights) {
        int currentPage = isLoadMore ? current + 1 : 1;
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map<String, Object> map = new HashMap<>();
        map.put("limit", 20);
        map.put("page", currentPage);
        map.put("provinceNo", no4);
        map.put("cityNo", no5);
        map.put("fieldClassId", no6); // 领域分类id
        map.put("honorNo", no3); // 企业经营类型code(对应前端行业分类筛选项)
        map.put("industryNameNo", ""); // 行业分类
        map.put("name", companyName); // 查询名称
        map.put("state", no7); // 商标、版权、专利状态:1授权已认证 2授权未认证
        map.put("type", no1); // 专利类型
        map.put("typesOfPropertyRights", typesOfPropertyRights); // 产权类型:传汉字
        repertory.getApi().getMemberIntellectualPropertyRight(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<MemberIntellectualPropertyBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<MemberIntellectualPropertyBean>>> data) {
                current = currentPage;
                int total = data.getData().getPages();
                fragment.setMemberIntellectualPropertyRight(data.getData().getList(), isLoadMore, current >= total);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<MemberIntellectualPropertyBean>>>> call, Throwable t) {
                super.onFailure(call, t);
                fragment.showEmpty();
            }
        });
    }

    //
    public void getPropertyType(int index, boolean isShowPopup) {
        Call<BaseEntity<List<CommonPopEntity>>> call = null;
        switch (index) {
            case 0: // 商标
                call = repertory.getApi().getTrademarkType();
                break;
            case 1: // 版权
                call = repertory.getApi().getRightType();
                break;
            case 2: // 专利
                call = repertory.getApi().getPatentType();
                break;
        }
        if (call == null) {
            return;
        }
        DialogHelper.showProgressDialog(fragment.getActivity(), "数据获取中...").setCanceledOnTouchOutside(false);
        call.enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData().size() > 0) {
                    for (CommonPopEntity datum : data.getData()) {
                        datum.setNo(datum.getTypeName());
                    }
                    switch (index) {
                        case 0: // 商标
                        case 1: // 版权
                            data.getData().add(0, new CommonPopEntity("", "不限"));
                            break;
                        case 2: // 专利
                            break;
                    }
                }
                fragment.setPropertyTypeList(isShowPopup, data.getData());
            }
        });
    }

    // 获取经营类型
    public void getServiceTypeList(boolean isShowPopup) {
        DialogHelper.showProgressDialog(fragment.getActivity(), "数据获取中...").setCanceledOnTouchOutside(false);
        repertory.getApi().getServiceTypeList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                fragment.setServiceTypeList(isShowPopup, data.getData());
            }
        });
    }

    // 获取专利-领域分类
    public void getFieldClassifyType(boolean isShowPopup) {
        DialogHelper.showProgressDialog(fragment.getActivity(), "数据获取中...").setCanceledOnTouchOutside(false);
        repertory.getApi().getFieldClassifyType().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                fragment.setFieldClassifyType(isShowPopup, data.getData());
            }
        });
    }

    // 获取专利-专利状态
    public void getPatentStatus(boolean isShowPopup) {
        DialogHelper.showProgressDialog(fragment.getActivity(), "数据获取中...").setCanceledOnTouchOutside(false);
        repertory.getApi().getPatentStatus().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                fragment.setPatentStatus(isShowPopup, data.getData());
            }
        });
    }

    /**
     * 获取省市区地址
     */
    public void getAddressModel(boolean isShowPopup) {
        DialogHelper.showProgressDialog(fragment.getContext(), "数据获取中...").setCanceledOnTouchOutside(false);
        repertory.getApi().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    List<CommonPopEntity> entities = new ArrayList<>();
                    for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> datum : data.getData()) {
                        entities.add(new CommonPopEntity(datum.getNo(), datum.getSimpleName()));
                    }
                    fragment.setAddressData(isShowPopup, data.getData(), entities);
                }
            }
        });
    }

    public void getUrlSetting(String param, String id) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getApi().getUrlSetting(param).enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                String url = data.getData().getUrl() + "?id=" + id;
                MemberConfig.getInstance().startWebViewX5(fragment.getActivity(), url, 101);
            }
        });
    }
}
